<template>
  <view class="containter">
    <view class="dayun-box">
      <view class="liuri-box">
        <view class="dayun-item first-title">
          <view class="dayun-year-title"></view>
          <view class="dayun-info-title">流日</view>
        </view>
        <view class="none-data" v-if="loading">
          <uni-load-more class="none-loading" color="#fff" status="loading" />
        </view>
        <template v-for="(item,index) in liuriData">
          <view class="dayun-item" :key="index" @click="liuriClick(item, index)">
            <view class="dayun-year">{{item.dayInChinese}}</view>
            <view class="dayun-info" :class="[liuriIndex == index ? 'dayun-select' : '',]">
              <view class="dayun-ele">
                {{item.ganZhi[0]}}
              </view>
              <view class="dayun-ele">
                {{item.ganZhi[1]}}
              </view>
            </view>
          </view>
        </template>
      </view>
    </view>
  </view>
</template>
<script>
  import {
    getBaziLiuRiApi
  } from "@/http/api";
  import wuxingColor from '@/utils/wuxing.js'
  import {
    EventBus
  } from '@/main.js'
  export default {
    props: {
      result: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        loading: false,
        baziResult: this.result,
        liuyueData: [],
        liunian: '',
        liuriIndex: undefined,
        liuriData: []
      }
    },
    mounted() {
      EventBus.$on('liuyue', param => {
        this.liuriIndex = 0;
        this.liuyueData = param.liuyueData;
        this.liunian = param.year;
        // this.getLiuriData(param.year, true);
        this.liuriData = param.liuyueData.customLiuRi
        this.$emit('change', this.liuriData[0]);
      })
    },
    beforeDestroy() {
      EventBus.$off("liuyue")
    },
    methods: {
      getLiuriData(year, isFirst) {
        this.loading = true;
        getBaziLiuRiApi({
          userName: this.baziResult.userName,
          birthDay: this.baziResult.birthDay,
          districtGeocode: this.baziResult.districtGeocode,
          sex: this.baziResult.sex,
          solar: (this.baziResult.trueSolarTime && this.baziResult.trueSolarTime != "") ? true : false,
          yearMonth: year + '-' + (this.liuyueData.index + 1)
        }).then(res => {
          if (res.code == 200) {
            // 补全行个数
            let liuriTem = res.data;
            this.liuriData = liuriTem;
            this.loading = false;
            if (isFirst) {
              this.$emit('change', this.liuriData[0]);
              EventBus.$emit('liuri', {
                liuriData: this.liuriData[0]
              })
            }
          }
        })
      },
      // 对应五行信息
      fiveElementInfo(str) {
        let {
          color
        } = wuxingColor.getJiuLongGanzhiWuxingColorInfo(str);
        return `color: ${color}`
      },
      liuriClick(data, index) {
        if (this.liuriIndex == index) return
        this.liuriIndex = index;
        this.$emit('change', data);
        EventBus.$emit('liuri', {
          liuriData: data
        })
      },
      liuriDate(date) {
        if (date) {
          let arr = date.split('-');
          return `${arr[1]}-${arr[2]}`
        }
        return '-'
      }
    }
  };
</script>

<style lang="scss" scoped>
  .containter {
    font-weight: bold;
    width: 100%;
    box-sizing: border-box;
    background-color: #eeeeee;
  }

  .containter {
    width: 100%;
    box-sizing: border-box;
    font-weight: bold;
  }

  .dayun-box {
    display: flex;
    flex-direction: row;
    box-sizing: border-box;
    position: relative;

    .none-data {
      position: absolute;
      width: 100%;
      height: 100%;

      .none-loading {
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.3);
      }
    }

    .liuri-box {
      display: flex;
      flex-direction: row;
      box-sizing: border-box;
      overflow-y: scroll;
    }
    .first-title {
      position: sticky;
      left: 0;
      background: #eeeeee;
      .dayun-year-title {
        width: 100%;
        background-color: #cccccc;
        height: 44rpx;
      }
      .dayun-info-title {
        padding: 14rpx 0;
        line-height: 64rpx;
      }
    }
    .dayun-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
      width: 10%;
      box-sizing: border-box;

      .dayun-year {
        width: 100%;
        font-size: 20rpx;
        text-align: center;
        background-color: #cccccc;
      }

      .dayun-info {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 14rpx;

        // margin: 8rpx;
        .dayun-ele {
          display: flex;
          flex-direction: row;
          line-height: 32rpx;

          .dayun-ganzhi {
            font-size: 40rpx;
            padding-right: 4rpx;
          }

          .dayun-shishen {
            font-size: 20rpx;
            color: #4B240D;
          }
        }
      }
    }
  }

  .dayun-select {
    background-color: #999999;
  }
</style>